package com.ruleengine.coordinator.config;

import com.ruleengine.coordinator.Coordinator;
import com.ruleengine.coordinator.ICoordinator;
import com.ruleengine.coordinator.IProducer;
import com.ruleengine.coordinator.SendWorkerClient;
import com.ruleengine.worker.rulechain.ChainManager;
import com.ruleengine.worker.runner.chain.ChainRunnerManager;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author lianghanwei on 2024/3/22
 */
@Configuration
@ConfigurationProperties(prefix = "re-coordinator")
@Data
public class ReCoordinatorConfig {
    private int clientNum;
    private String topicFilter;
    private String groupName;
    private boolean ordered;
    private String userName;
    private String password;
    private boolean cleanStart;
    private long sessionExpiryInterval;
    private String host;
    private int port;


    @Bean
    public SendWorkerClient sendWorkerClient(ChainManager chainManager, ChainRunnerManager chainRunnerManager) {
        return new SendWorkerClient(chainManager, chainRunnerManager);
    }

    @Bean
    public ICoordinator getCoordinator(IProducer producer) {
        Coordinator coordinator = Coordinator.builder()
                .producer(producer)
                .host(host)
                .port(port)
                .clientNum(clientNum)
                .cleanStart(cleanStart)
                .sessionExpiryInterval(sessionExpiryInterval)
                .userName(userName)
                .password(password)
                .topicFilter(topicFilter)
                .groupName(groupName)
                .ordered(ordered)
                .build();
        coordinator.start();
        return coordinator;
    }
}
